# coding: utf-8
from pathlib import Path
from typing import Union

from .engin_base import FileEngineBase
from .excel_engine import ExcelEngine
from .json_engine import JsonEngine, json_write, json_read
from .yaml_engine import YamlEngine


class FileEngine:

    def __init__(self, fileName: Union[str, Path], **kwargs):
        fileName = Path(fileName).absolute() if isinstance(fileName, str) else fileName
        suffix = fileName.suffix
        if suffix in ['.xlsx', '.xls']:
            self.engine = ExcelEngine(fileName, **kwargs)
        elif suffix == '.json':
            self.engine = JsonEngine(fileName, **kwargs)
        elif suffix in ['.yaml', '.yml']:
            self.engine = YamlEngine(fileName, **kwargs)
        else:
            raise ValueError(f"Unsupported file type: {suffix}")

    def read(self):
        return self.engine.read()

    def getEnableData(self):
        return self.engine.getEnableData()

    def ApiTemplate(self, fileName: Union[str, Path]):
        """
        API模板
        :param fileName:
        :return:
        """
        return self.engine.ApiTemplate(fileName)

    def UiTemplate(self, fileName: Union[str, Path]):
        """
        UI模板
        :param fileName:
        :return:
        """
        return self.engine.UiTemplate(fileName)
